home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ccdl150l.zip / DATA / ATOI.C < prev    next >
C/C++ Source or Header  |  1996-07-02  |  651b  |  45 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. long strtol(const char *s, char **endptr, int radix)
  5. {
  6.     int sign = 0;
  7.     int val = 0;
  8.  
  9.     while (isspace(*s)) s++;
  10.  
  11.     if (*s == '-') {
  12.         sign++;
  13.         s++;
  14.     }
  15.     else if (*s == '+') 
  16.         s++;
  17.     if (*s == '0') {
  18.         radix = 8;
  19.         s++;
  20.         if (*s == 'x' || *s == 'X') {
  21.             radix = 16;
  22.             s++;
  23.         }
  24.     }
  25.     while(isalnum(*s)) {
  26.         unsigned temp = toupper(*s++)- '0';
  27.         if (temp >= 10)
  28.             temp -= 7;
  29.         if (temp >=radix) {
  30.             s--;
  31.             break;
  32.         }
  33.         val*= radix;
  34.         val += temp;
  35.     }
  36.     if (sign)
  37.         val = -val;
  38.     if (endptr)
  39.         *endptr = s;
  40.     return val;
  41. }
  42. long atol(const char *s)
  43. {
  44.     return strtol(s,0,10);
  45. }